﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Mogre;

using System.Drawing;
using System.Runtime.InteropServices;

namespace ProjetSMA.View.Mogre
{
    public class Prey3D : Entity3D
    {

        public Prey3D(Map3D par_copy_world, Model.ItemMove par_item, Root par_root, SceneManager par_sc, string par_name, int par_l)
            : base(par_copy_world, par_item, par_root, par_sc, par_name, par_l)
        {
            m_item = par_item;
            m_world = par_copy_world;
            m_copy_world = par_copy_world.Elements3D;
            m_root = par_root;
            m_scnmgr = par_sc;
            m_name = par_name;
            m_length_item = par_l;
            m_pattern = new Pattern3D(m_root, m_scnmgr, m_name + "-pattern3D");

            m_scale_max = 1f;

            
            // OGC
            m_scale_step = (float)(double)m_scale_max / m_item.GrowthMax;
            m_scale = m_item.Growth * m_scale_step;
           

        }

        public override void UpdateGrowth()
        {
            m_scale = m_item.Growth * m_scale_step;
            m_scale += 1.3f;
            m_node.SetScale(new Vector3(m_scale, m_scale, m_scale));
        }


        public override void UpdateChildren()
        {

            if (m_compt_child == m_item.Children.Count)
            {
                Prey3D child = new Prey3D(m_world,
                                          m_item.Children[m_item.Children.Count - 1],
                                          m_root, m_scnmgr,
                                          m_name + "_child_prey" + m_item.Children.Count,
                                          m_length_item);
                child.Initialisation();
                m_compt_child++;
                m_world.ElementsMove.Add(child);

                is_breeding = true;
            }

        }



        public void InitialisationBillboard()
        {

            m_billboard_set = m_scnmgr.CreateBillboardSet(m_name + "_bill");
            m_billboard_set.SetMaterialName("Examples/ElementField3DQuint");

            m_billboard = m_billboard_set.CreateBillboard(new Vector3(0, 14, 0));
            m_billboard_set.Visible = true;
            m_billboard.SetDimensions(7, 7);

            m_node.AttachObject(m_billboard_set);
        }



        public void Initialisation()
        {
            m_name += "ogre_";
            m_scale += 1.3f;
            m_root.FrameStarted += new FrameListener.FrameStartedHandler(Skeletal_FrameStarted);
            // Setup animation default
            Animation.DefaultInterpolationMode = Animation.InterpolationMode.IM_LINEAR;
            Animation.DefaultRotationInterpolationMode = Animation.RotationInterpolationMode.RIM_LINEAR;

            
            Random rnd = new Random();

            ent = m_scnmgr.CreateEntity(m_name, "Sinbad.mesh");

            // make sure our animations add their weights and don't just average out. The AnimationBlender already handles averaging between two anims.
           // Skeleton skeleton = ent.Skeleton;
           // skeleton.BlendMode = SkeletonAnimationBlendMode.ANIMBLEND_CUMULATIVE;


            /*
             *  {"IdleBase", "IdleTop", "RunBase", "RunTop", "HandsClosed", "HandsRelaxed", "DrawSwords",
                "SliceVertical", "SliceHorizontal", "Dance", "JumpStart", "JumpLoop", "JumpEnd"};
             * */

            _animState = ent.GetAnimationState("RunTop");
            _animState.Enabled = true;
            _animState.Loop = true;
            _animationSpeed = (float)(rnd.NextDouble() + 0.5);


            _animState_bot = ent.GetAnimationState("RunBase");
            _animState_bot.Enabled = true;
            _animState_bot.Loop = true;


            m_node = m_scnmgr.RootSceneNode.CreateChildSceneNode(m_name, new Vector3(m_item.PositionNow.I * m_length_item,
                                                                                     5 * m_item.PositionNow.Height + 11f,
                                                                                     m_item.PositionNow.J * m_length_item));
            m_node.SetScale(new Vector3(m_scale, m_scale, m_scale));
            ent.CastShadows = true;
            m_node.AttachObject(ent);

            m_node.Yaw(new Degree(90));

            m_decalage = 0f;
            m_dec = 15;
            m_decalage_speed = 0.4f;
            InitialisationBillboard();
        }


    }
}



